import json
import cv2
import numpy as np

def fit_ellipse(keypoints):
    # 将关键点转换为numpy数组
    points = np.array(keypoints, dtype=np.float32)

    # 使用OpenCV拟合椭圆
    ellipse = cv2.fitEllipse(points)

    # 返回椭圆的中心
    return ellipse[0]

def pad_image(img):
    height, width = img.shape[:2]
    if width >= height:
        border_width = int((width - height) / 2)
        img = cv2.copyMakeBorder(img, border_width, border_width, 0, 0, cv2.BORDER_CONSTANT, value=0)
        return img, border_width, 0
    else:
        border_height = int((height - width) / 2)
        img = cv2.copyMakeBorder(img, 0, 0, border_height, border_height, cv2.BORDER_CONSTANT, value=0)
        return img, 0, border_height


with open("data/data.json", "r") as f:
    data = json.load(f)

image = cv2.imread("data/datad.jpg")
# image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)

# image, pad_width, pad_height = pad_image(image)

# image = cv2.resize(image, (390, 844))

h, w, _ = image.shape
vertex = np.asarray(data['projected'])
# vertex = np.asarray(data['capProjected'])
print(vertex)
vertex[:, 0] /= 390
vertex[:, 1] /= 844
vertex[:, 0] *= w
vertex[:, 1] *= h
#
# vertex[:, 0] += pad_height*2.15

# print(vertex)
# vertex[:, 0] = w - vertex[:, 0]

# image = np.zeros((844, 390, 3), dtype=np.uint8)

for idx, (x, y) in enumerate(vertex.astype(int)):
    cv2.circle(image, (x, y), radius=1, color=(0, 255, 0), thickness=2)
    # cv2.putText(image, str(idx), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)

left_eyes = range(1085, 1109)
right_eyes = range(1061, 1085)
for idx, (x, y) in enumerate(vertex.astype(int)[left_eyes]):
    cv2.circle(image, (x, y), radius=1, color=(0, 0, 255), thickness=2)

for idx, (x, y) in enumerate(vertex.astype(int)[right_eyes]):
    cv2.circle(image, (x, y), radius=1, color=(0, 0, 255), thickness=2)

l_c = fit_ellipse(vertex[left_eyes])
r_c = fit_ellipse(vertex[right_eyes])
print(l_c)
cv2.circle(image, (int(l_c[0]), int(l_c[1])), radius=1, color=(0, 0, 255), thickness=5)
cv2.circle(image, (int(r_c[0]), int(r_c[1])), radius=1, color=(0, 0, 255), thickness=5)

cv2.imshow("w", image)
cv2.waitKey(0)

# idx.png

left_eyes = [358, 426, 48, 47, 40, 41, 42, 45, 44, 43, 356, 416, 46, 417, 56, 55, 53, 54, 50, 51, 52, 169, 171, 357,
             1193, 1194, 1195, 1196 ,1197, 1198, 1199, 1200, 1201, 1202, 1203, 1204, 1181, 1182, 1183, 1184, 1185, 1186, 1187, 1188, 1189, 1190, 1191, 1192,
             1089, 1088, 1087, 1086, 1085, 1108, 1107, 1106, 1105, 1104, 1103, 1102, 1101, 1100, 1099, 1098, 1097, 1096, 1095, 1094, 1093, 1092, 1091, 1090]
right_eyes = [789, 854, 498, 497, 490, 491, 492, 495, 494, 493, 787, 846, 496, 847, 506, 505, 503, 504, 500, 501, 502, 618, 620, 788,
              1168, 1167, 1166, 1165, 1164, 1163, 1162, 1161, 1160, 1159, 1158, 1157, 1180, 1179, 1178, 1177, 1176, 1175, 1174, 1173, 1172, 1171, 1170, 1169,
              1081, 1082, 1083, 1084, 1061, 1062, 1063, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071, 1072, 1073, 1074, 1075, 1076, 1077, 1078, 1079, 1080]

assert len(left_eyes) == len(right_eyes)

np.save("eyes_index.npy", np.asarray(left_eyes + right_eyes))
print(np.asarray(left_eyes + right_eyes).shape)

new_w, new_h = w * 3, h * 3
vertex[:, 0] /= w
vertex[:, 1] /= h
vertex[:, 0] *= new_w
vertex[:, 1] *= new_h
ones = np.ones((new_h, new_w, 3), dtype=np.uint8)
for idx, (x, y) in enumerate(vertex.astype(int)):
    if idx in left_eyes + right_eyes:

        cv2.circle(ones, (x, y), radius=1, color=(0, 0, 255), thickness=2)
        cv2.putText(ones, str(idx), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
        pass
    else:
        cv2.circle(ones, (x, y), radius=1, color=(0, 255, 0), thickness=2)
        cv2.putText(ones, str(idx), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)


cv2.imwrite("ldx_open_eye.jpg", ones)